import $U from '@/common/free-lib/util.js';
import $H from '@/common/free-lib/request.js';
export default {
	state:{
		// 全局数据
		columnList:[],
		// 录音管理器
		categoryList:[]
	},
	mutations: {
		initHome(state) {
			$H.get('/infra/wxIndex/home').then(res => {
				state.columnList = res
				$U.setStorage('COLUMN_LIST',JSON.stringify(res))
			})
			
			$H.get('/product/category/getCategoryList').then(res => {
				state.categoryList = res
				$U.setStorage('CATEGORY_LIST',JSON.stringify(res))
			})
			
			if($U.isLogin()) {
				// setInterval(() => {
				// 	$H.get('/trade/cart/get-count').then(res => {
				// 		if(res && res > 0) {							
				// 			uni.setTabBarBadge({
				// 			  index: 2,
				// 			  text: res + ''
				// 			})
				// 		}
				// 	})
				// 	$H.get('/member/msg/getUnread').then(res => {
				// 		if(res && res.total > 0) {							
				// 			uni.setTabBarBadge({
				// 			  index: 1,
				// 			  text: res.total + ''
				// 			})
				// 		}
				// 	})
				// }, 1000);
				$H.get('/trade/cart/get-count').then(res => {
					if(res && res > 0) {						
						if(res > 0) {
							uni.setTabBarBadge({
							  index: 2,
							  text: res + ''
							})
						}
					}
					else {
						uni.removeTabBarBadge({
						  index: 2
						})
					}
				})
				$H.get('/member/msg/getUnread').then(res => {
					if(res && res.total > 0) {	
						uni.setTabBarBadge({
						  index: 1,
						  text: res.total + ''
						})					
					}
					else {
						uni.removeTabBarBadge({
						  index: 1
						})
					}
				})
			}
		}
	},
	actions: {
		getColumnList({state,dispatch}) {
			if(state.columnList == null || state.columnList.length == 0) {
				state.columnList = JSON.parse($U.getStorage('COLUMN_LIST'));
			}
			if(state.columnList == null || state.columnList.length == 0) {
				dispatch('initHome');
			}
			return state.columnList
		}
	}
}